package com.oil.business.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.oil.business.domain.TActivityInfo;
import com.oil.business.service.ITActivityInfoService;
import com.oil.business.vo.TActivityInfoVo;
import com.oil.common.core.domain.model.LoginUser;
import com.oil.common.exception.ServiceException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.oil.common.annotation.Log;
import com.oil.common.core.controller.BaseController;
import com.oil.common.core.domain.AjaxResult;
import com.oil.common.enums.BusinessType;
import com.oil.common.utils.poi.ExcelUtil;
import com.oil.common.core.page.TableDataInfo;

/**
 * 活动Controller
 *
 * @author ruoyi
 * @date 2023-01-05
 */
@RestController
@RequestMapping("/activity/info")
public class TActivityInfoController extends BaseController {
    @Autowired
    private ITActivityInfoService tActivityInfoService;

    /**
     * 查询活动列表
     */
//    @PreAuthorize("@ss.hasPermi('activity:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(TActivityInfo tActivityInfo) {
        startPage();
        List<TActivityInfoVo> list = tActivityInfoService.selectTActivityInfoList1(tActivityInfo);
        return getDataTable(list);
    }

    /**
     * 导出活动列表
     */
    @PreAuthorize("@ss.hasPermi('activity:info:export')")
    @Log(title = "活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TActivityInfo tActivityInfo) {
        List<TActivityInfo> list = tActivityInfoService.selectTActivityInfoList(tActivityInfo);
        ExcelUtil<TActivityInfo> util = new ExcelUtil<TActivityInfo>(TActivityInfo.class);
        util.exportExcel(response, list, "活动数据");
    }

    /**
     * 获取活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('activity:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tActivityInfoService.selectTActivityInfoById(id));
    }
    /**
     * 提交审批
     */
//    @PreAuthorize("@ss.hasPermi('activity:info:list')")
    @GetMapping("/publish/{id}")
    public AjaxResult publish(@PathVariable("id") Long id) {
        int publish = tActivityInfoService.publish(id);
//        throw new ServiceException("已经在审批中了");
        return AjaxResult.success();
    }
    /**
     * 新增活动
     */
    @PreAuthorize("@ss.hasPermi('activity:info:add')")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TActivityInfoVo tActivityInfo) {
        LoginUser loginUser = getLoginUser();
        tActivityInfo.setCrtBy(loginUser.getUsername());
        tActivityInfo.setUpdBy(loginUser.getUsername());
        return toAjax(tActivityInfoService.insertTActivityInfo(tActivityInfo,loginUser));
    }

    /**
     * 修改活动
     */
    @PreAuthorize("@ss.hasPermi('activity:info:edit')")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TActivityInfo tActivityInfo) {
        LoginUser loginUser = getLoginUser();
        tActivityInfo.setUpdBy(loginUser.getUsername());
        tActivityInfo.setUpdTime(new Date());
        return toAjax(tActivityInfoService.updateTActivityInfo(tActivityInfo));
    }

    /**
     * 删除活动
     */
    @PreAuthorize("@ss.hasPermi('activity:info:remove')")
    @Log(title = "活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (long id : ids) {
            TActivityInfo tActivityInfo = tActivityInfoService.selectTActivityInfoById(id);
            LoginUser loginUser = getLoginUser();
            tActivityInfo.setUpdBy(loginUser.getUsername());
            tActivityInfo.setUpdTime(new Date());
            tActivityInfo.setDelFlag(1);
            tActivityInfoService.updateTActivityInfo(tActivityInfo);
        }
        return success();
    }
}
